package com.seckill.Service.Impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seckill.Cache.HotKeyLocalCache;
import com.seckill.Common.BaseResponse;
import com.seckill.Mapper.GoodsMapper;
import com.seckill.Model.DO.Goods;
import com.seckill.Service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private HotKeyLocalCache hotKeyLocalCache;

    @Override
    public BaseResponse getGoodsPage(Integer pageNo, Integer pageSize) {
        IPage<Goods> goodsPage = new Page<>(pageNo, pageSize);
        IPage<Goods> result = goodsMapper.selectPage(goodsPage, null);
        return BaseResponse.success(result);
    }

    @Override
    public Goods getGoodsById(Long id) {
        String goods = hotKeyLocalCache.getGoodsInfoByLocalCache(id);
        if(!Objects.isNull(goods)){
            return JSON.parseObject(goods,Goods.class);
        }
        // TODO 查询redis

        return goodsMapper.selectById(id);
    }
}
